package com.alog.yzd.bookmall.web.servlet.admin.order;

import com.alog.yzd.bookmall.domain.order.Order;
import com.alog.yzd.bookmall.domain.user.User;
import com.alog.yzd.bookmall.pager.PageBaen;
import com.alog.yzd.bookmall.service.order.OrderService;
import com.alog.yzd.bookmall.utils.WebUtils;
import com.alog.yzd.commons.Print;
import com.alog.yzd.commons.servlet.BaseServlet;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @Auther: Ye Zhengdong
 * @Date: 2019/1/12 19:49
 * @Description:
 */
public class AdminOrderServlet extends BaseServlet {
    private OrderService orderService = new OrderService();
    /**
     * 发货
     */
    public String diliver(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String oid = req.getParameter("oid");
        int status = orderService.findStatus(oid);
        if(status == 2){
            orderService.updateStatus(oid, 3);
            req.setAttribute("code", "success");
            req.setAttribute("msg", "发货成功");
            return "f:/adminjsps/admin/msg.jsp";
        } else {
            req.setAttribute("code", "error");
            req.setAttribute("msg", "状态不对, 不能发货");
            return "f:/adminjsps/admin/msg.jsp";
        }
    }
    /**
     * 取消订单
     */
    public String cancel(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String oid = req.getParameter("oid");
        int status = orderService.findStatus(oid);
        if(status == 1){
            orderService.updateStatus(oid, 5);
            req.setAttribute("code", "success");
            req.setAttribute("msg", "订单取消成功");
            return "f:/adminjsps/admin/msg.jsp";
        } else {
            req.setAttribute("code", "error");
            req.setAttribute("msg", "状态不对, 不能取消");
            return "f:/adminjsps/admin/msg.jsp";
        }
    }

    /**
     * 加载订单
     */
    public String load(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String oid = req.getParameter("oid");
        Order order = orderService.load(oid);
        req.setAttribute("order", order);
        String btn = req.getParameter("btn");
        req.setAttribute("btn", btn);
        return "f:/adminjsps/admin/order/desc.jsp";
    }

    /**
     * 按状态查询订单
     */
    public String findByStatus(HttpServletRequest req, HttpServletResponse resp){
        int pc = WebUtils.getPc(req);
        String url = WebUtils.getUri(req);
        int status = Integer.parseInt(req.getParameter("status"));
        PageBaen<Order> pb = orderService.findByStatus(status,pc);
        pb.setUrl(url);
        req.setAttribute("pb", pb);
        Print.print("findAll, pb:" + pb);
        return "f:/adminjsps/admin/order/list.jsp";
    }

    /**
     * 我的订单
     * @param req
     * @param resp
     * @return
     * @throws ServletException
     * @throws IOException
     */
    public String findAll(HttpServletRequest req, HttpServletResponse resp){
        int pc = WebUtils.getPc(req);
        String url = WebUtils.getUri(req);
        PageBaen<Order> pb = orderService.findAll(pc);
        pb.setUrl(url);
        req.setAttribute("pb", pb);
        Print.print("findAll, pb:" + pb);
        return "f:/adminjsps/admin/order/list.jsp";
    }

}
